package com.lagou.module.dao;

import com.lagou.module.entity.ClassGrade;
import com.lagou.module.entity.PageBean;
import com.lagou.module.util.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

public class ClassGradeDaoImpl implements ClassGradeDao {

    @Override
    public List<ClassGrade> classGradeShow(PageBean pageBean) {
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());

        StringBuilder stringBuilder = new StringBuilder(" select * from t_classGrade ");
        if (pageBean != null){
            stringBuilder.append(" limit " + pageBean.getStart() + "," + pageBean.getRow() );
        }
        //QueryRunner 不能直接用  StringBuilder 对象   需要先转化为String类型对象
        String sql = stringBuilder.toString();


        try {
            List<ClassGrade> grades = queryRunner.query(sql, new BeanListHandler<ClassGrade>(ClassGrade.class));
            return grades;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    //Add a class
    @Override
    public int classGradeAdd(ClassGrade classGrade) {

        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());

        String sql = " insert into t_classGrade values (null, ?, ?, ?, ?, ?) ";

        try {
            int i = queryRunner.update(sql, classGrade.getClassName(), classGrade.getGradeName(), classGrade.getHeadTeacherName(), classGrade.getClassSlogan(), classGrade.getClassPopulation());
            return i;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }


    //delete a class
    @Override
    public int classGradeDelete(String[] ids) {
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());

        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < ids.length; i++ ){
            if (i == ids.length-1){
                sb.append(ids[i]);
            } else {
                sb.append(ids[i]).append(",");
            }
        }
        System.out.println(sb);

        String sql = " delete from t_classgrade where classId in (" +sb+" ) ";
        try {
            int i = queryRunner.update(sql);
            return i;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }


    //Modify
    @Override
    public int classGradeModify(ClassGrade classGrade) {
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());

        String sql = "update t_classGrade set className = ? , gradeName = ?, headteacherName = ?, classSlogan = ?, classPopulation = ? where classId = ? ";

        try {
            int i = queryRunner.update(sql, classGrade.getClassName(), classGrade.getGradeName(), classGrade.getHeadTeacherName(),classGrade.getClassSlogan(), classGrade.getClassPopulation(), classGrade.getClassId());
            return i;
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return 0;

    }


    //Find
    @Override
    public ClassGrade classGradeFind(int classId) {

        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());

        String sql = "select * from t_classGrade where classId = ?";
        try {
            ClassGrade grade = queryRunner.query(sql, new BeanHandler<ClassGrade>(ClassGrade.class), classId);
            return grade;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
